﻿@using System.Diagnostics
@using System.Globalization
@using Orchard.CultureSwitcher.Models

@{
    var availableCultures = Model.AvailableCultures as List<string>;
    var settings = Model.Settings as SwitcherSettingsPart;
    
    if (settings.DisplayMode == 0)
    {
        settings.DisplayMode = (int) DisplayModes.DropdownList;
    }

    var availableCultureswithNames = new List<KeyValuePair<string, string>>(availableCultures.Count);
    foreach (string culture in availableCultures)
    {
        try
        {
            var cultureInfo = new CultureInfo(culture);
            string name = culture;
            switch ((DisplayTypes)settings.DisplayType)
            {
                case DisplayTypes.Language:
                    name = cultureInfo.IsNeutralCulture ? cultureInfo.EnglishName : cultureInfo.Parent.EnglishName;
                    break;
                case DisplayTypes.NativeLanguage:
                    name = cultureInfo.IsNeutralCulture ? cultureInfo.NativeName : cultureInfo.Parent.NativeName;
                    break;
                case DisplayTypes.LanguageWithCountry:
                    name = cultureInfo.DisplayName;
                    break;
                case DisplayTypes.NativeLanguageWithCountry:
                    name = cultureInfo.NativeName;
                    break;
                case DisplayTypes.TwoLetterLanguage:
                default:
                    name = cultureInfo.TwoLetterISOLanguageName;
                    break;
            }
            availableCultureswithNames.Add(new KeyValuePair<string, string>(culture, name));
        }
        catch
        {
            availableCultureswithNames.Add(new KeyValuePair<string, string>(culture, culture));
        }
    }
}

@using (Html.BeginForm("ChangeCulture", "UserCulture", new { Area = "Orchard.CultureSwitcher" }, FormMethod.Post))
{
    @Html.AntiForgeryToken()

    if (settings.DisplayMode == (int)DisplayModes.DropdownList)
    {
        <div class="culture-switcher-container">
            @Html.DropDownList("CultureName", new SelectList(availableCultureswithNames, "Key", "Value", Model.UserCulture), new { onchange = "submit()" })
        </div>
    }

    if (settings.DisplayMode == (int)DisplayModes.BulletedList)
    {
        <div class="culture-switcher-container">
            @Html.Hidden("CultureName")
            <ul>
                @foreach (var culture in availableCultureswithNames)
                {
                    <li><a data-culture="@culture.Key">@culture.Value</a></li>
                }
            </ul>
        </div>

        using (Script.Foot())
        {
            Script.Require("jQuery").AtFoot();

            <script>
                $(function () {
                    $('.culture-switcher-container a').click(function () {
                        $('#CultureName').val($(this).attr('data-culture'));
                        $(this).closest('form').submit();
                    });
                })
            </script>
        }
    }
}

